package org.car.world.common.util;

import java.util.ArrayList;
import java.util.List;

public class Page<T> {

    private int currentPage = 1;// 起始页
    private int pageSize = 0;// 显示的条数

    private int totalPage;// 总页数
    private int totalRow;// 总数
    private int prevPage;// 上一页
    private int nextPage;// 下一页
    private List<T> PageList; //数据集合

    /**
     * @param @return 参数
     * @return int    返回类型
     * @throws
     * @Title: getCurrentPageForm
     * @Description:获取到初始的也码
     */
    public int getCurrentPageForm() {
        return (this.currentPage - 1) * this.pageSize;
    }


    /**
     * 显示一组页数
     */
    private int groupPageCount = 5;// 一组显示的页数
    private List<Integer> groupPage = new ArrayList<Integer>();// 鏄剧ず缁勯〉鏁�

    public List<T> getPageList() {
        return PageList;
    }

    public void setPageList(List<T> pageList) {
        PageList = pageList;
    }

    public Page() {
    }

    public Page(int currentPage, int pageSize) {
        super();
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }


    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotalRow() {
        return totalRow;
    }

    /**
     * 璁剧疆鎬昏鏁�
     *
     * @param totalRow
     */
    public void setTotalRow(int totalRow) {
        this.totalRow = totalRow;

        // 计算总页数
        if (this.totalRow % this.pageSize == 0) {
            this.totalPage = this.totalRow / this.pageSize;
        } else {
            this.totalPage = this.totalRow / this.pageSize + 1;
        }
    }

    /**
     * 计算上一页
     *
     * @return
     */
    public boolean getHasPrevPage() {

        return this.currentPage - 1 > 0;
    }

    /**
     * 上一页
     *
     * @param prevPage
     */
    public int getPrevPage() {
        if (this.currentPage - 1 > 0) {
            this.prevPage = this.currentPage - 1;
        }
        return prevPage;
    }

    /**
     * 计算下一页
     *
     * @return
     */
    public boolean getHasNextPage() {
        return this.currentPage + 1 <= this.totalPage;
    }

    /**
     * 下一页
     *
     * @return
     */
    public int getNextPage() {

        if (this.currentPage + 1 <= this.totalPage) {
            this.nextPage = this.currentPage + 1;
        }
        return nextPage;
    }


    public void setPrevPage(int prevPage) {
        this.prevPage = prevPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }


    public int getTotalPage() {
        return totalPage;
    }

    /**
     * 鏄剧ず涓�缁勭殑椤垫暟
     *
     * @return
     */
    public List<Integer> getGroupPage() {

        //瀹氫箟璧峰椤� - 缁撴潫椤�
        int startPage = 1, endPage = this.groupPageCount;

        //鍦ㄥ綋鍓嶉〉涔嬪墠鍑犻〉
        int firstPage = groupPageCount / 2;
        //鍦ㄥ綋鍓嶉〉涔嬪悗鍑犻〉
        int lastPage = groupPageCount - firstPage;

        //璁＄畻璧峰椤�
        if ((this.currentPage - firstPage) > 0 && this.totalPage - this.currentPage >= lastPage) {
            startPage = this.currentPage - firstPage + 1;
        } else if ((this.currentPage - firstPage) > 0 && this.totalPage - this.currentPage < lastPage) {
            startPage = this.totalPage - this.groupPageCount + 1;
        }

        //閲嶆柊璁＄畻闃叉鍑虹幇涓鸿礋
        if (startPage < 1) {
            startPage = 1;
        }

        //璁＄畻缁撴潫椤�
        if (this.totalPage - this.currentPage >= lastPage && startPage == 1 && this.totalPage >= this.groupPageCount) {
            endPage = this.groupPageCount;
        } else if (this.totalPage - this.currentPage >= lastPage && this.totalPage >= this.groupPageCount) {
            endPage = this.currentPage + lastPage;
        } else {
            endPage = this.totalPage;
        }

        //浠庤捣濮嬪埌缁撴潫娣诲姞闆嗗悎
        for (int i = startPage; i <= endPage; i++) {
            groupPage.add(i);
        }

        return groupPage;
    }


}
